package com.hereway.core.service;

import java.io.Serializable;
import java.util.List;

import com.hereway.core.context.PageBean;
import com.hereway.core.dao.IBaseDAO;
import com.hereway.core.exception.ServiceException;

/**
 * IService基本实现
 * @author kevin
 *
 * @param <T>
 * @param <PK>
 */
public abstract class BaseServiceImpl<T, PK extends Serializable> implements
		IService<T, PK> {

	protected abstract IBaseDAO<T, PK> getDAO();

	@Override
	public T load(PK pk) throws ServiceException {
		return getDAO().load(pk);
	}
	
	@Override
	public T get(PK pk) throws ServiceException {
		return getDAO().get(pk);
	}

	@Override
	public List<T> loadAll() throws ServiceException {
		return getDAO().loadAll();
	}
	
	@Override
	public List<T> loadAll(PageBean page) throws ServiceException {
		return getDAO().loadAll(page);
	}

	@Override
	public void save(T t) throws ServiceException {
		getDAO().save(t);
	}

	@Override
	public void delete(PK pk) throws ServiceException {
		T t = getDAO().load(pk);
		if (t != null)
			getDAO().delete(pk);
		else
			throw new ServiceException("no entity of pk is " + pk.toString());
	}

	@Override
	public void delete(PK[] pks) throws ServiceException {
		for (PK pk : pks) {
			delete(pk);
		}
	}
	
	@Override
	public void delete(T entity) throws ServiceException {
		getDAO().delete(entity);
	}
	
	@Override
	public void delete(List<T> list) throws ServiceException {
		getDAO().delete(list);
	}
	
	@Override
	public void update(T t) throws ServiceException {
		getDAO().update(t);
	}
}
